m.controller("editCommCtrl",function($rootScope,$scope,$location) {
	
	
	$scope.init = function(){	
		
		$scope.edit_id = $rootScope.getParam("id");
	    $scope.pic = "url";
		$scope.edit_name = "";
	    $scope.edit_price = "";
	    $scope.edit_picture = "";
	    $scope.edit_summary = "";
	    $scope.edit_description = "";
	    $scope.newimage ="";
	    $scope.edit_isSelled ="";
	    
	    
		$.ajax({
	            type:"POST",
	            url:"/commodity/getCommById",
	            data:{"id":$scope.edit_id},
	            contentType:"application/x-www-form-urlencoded",
	            dataType:"json",
	            success:function(data){
	            	if(data.value.isSelled){
	            		$.ajax({
	                        type:"POST",
	                        url:"/selled/getSelledByCommId",
	                        data:{"id":data.value.id},
	                        contentType:"application/x-www-form-urlencoded",
	                        dataType:"json",
	                        success:function(data){
	                        }
	                        });
	            	}
	            	 $scope.$apply(function(){
	            		 $scope.image = data.value.picture;
	            		 $scope.fileName = data.value.picture;
	            		 $scope.name = data.value.name;
	            		 $scope.price = data.value.price;
	            		 $scope.summary = data.value.summary;
	            		 $scope.description = data.value.description;
	            		 $scope.edit_isSelled =data.value.isSelled;
	                     });
	                 }
	            	
	            		
	            	});

		}
			

$scope.htmlInit = function(){

	
	
            $scope.init(); 
}

$scope.init();

var filePicture="";

$scope.editComm = function () {

	if($scope.pic == "url"){
		if($scope.newimage!=""){
		    $scope.edit_picture =  $scope.newimage;}else{
		    	$scope.edit_picture =$scope.image;
		    }
	}else if($scope.pic == "file"){
		$scope.edit_picture = $scope.fileName;
	}
	$scope.edit_name = $scope.name;
    $scope.edit_price = $scope.price;
    $scope.edit_summary = $scope.summary;
    $scope.edit_description = $scope.description;
    
	if($scope.edit_picture==""||$scope.edit_picture== null){
		
		$scope.htmlInit;
	}else{
		
			
		$.ajax({
        type: "POST",
        url: "/commodity/editComm",
        data: {"id":$scope.edit_id,"name": $scope.edit_name ,"price":$scope.edit_price,"picture":$scope.edit_picture,
            "summary":$scope.edit_summary,"description":$scope.edit_description,"isSelled":$scope.edit_isSelled},
        contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        success: function (data) {
        	if(data.success){
        		location.href = "/editSubmit?id="+$scope.edit_id;
        	}else{
        		alert("错误");
        	}
        }
    });

		
	}
};

$scope.fileChanged = function(obj){
	filePicture = "/resources/images/"+
    	    obj.value.substring(obj.value.lastIndexOf("\\")+1);
  
}
/**
 * 上传图片
 * @type {boolean}
 */
$scope.doUpload = function () { 
	var form = new FormData();
	var vfile = $("#uploadfile");
	var vfiles = vfile[0].files[0];
	var maxAllowedSize = 1000000;
	
	if(vfiles.size > maxAllowedSize) {
		alert("超过文件上传大小限制");
	}else{
		form.append('uploadfile', vfiles, vfiles.name);
		form.enctype = "multipart/form-data";
    	 $.ajax({
        	        url: '/commodity/addCommPicture' ,
        	        type: 'POST',
        	        data: form,
                    async: false,  
                    cache: false,  
                    contentType: false,  
                    processData: false,
                    dataType: 'json', //返回值类型，一般设置为json、application/json
                    success: function (returndata) {
                    	
        	            alert("图片上传成功"); 
        	            $scope.fileName = filePicture;
             	        
        	        },
        	        error: function (returndata) {
        	            alert("图片上传失败");
        	        }
        	    });
    	 }
            
        }

   
$scope.showPic = function (obj) {
	$scope.$apply(function(){
		$scope.image = obj.value;
		$scope.newimage = obj.value;
	});
}

});
